package model {
	import flash.events.ErrorEvent;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.mxml.HTTPService;
	
	[Event(type="controller.ServiceEvent", name="SE1")]
	[Event(type="controller.ServiceEvent", name="SE2")]
	[Event(type="controller.SearchEvent", name="SE3")]
	
	public class Service extends EventDispatcher {
		private var validateService:HTTPService;
		private var URL:String ;
		private static const APIKEY:String = "F28A515607A21CA9";
		private var operation:String;
		
		public function Service(url:String) {
			validateService = new HTTPService();
			URL = url;
			validateService.url = URL;
			validateService.resultFormat = "e4x"; // Format XML
			validateService.addEventListener(ResultEvent.RESULT, ResultHandler);
			validateService.addEventListener(FaultEvent.FAULT, FaultHandler);
		}
		
        public function getResultTop():void{
            trace("appel de getFavoris");
            validateService.cancel();
            validateService.url = "http://valentin.lecavelier.free.fr/getFavoris.php";
            operation = "gf";
            validateService.send();
        }
        
        public function getResultPlanning(toSend:String):void{
            trace("appel de getPlanning");
            validateService.cancel();
            validateService.url = "http://valentin.lecavelier.free.fr/getPlanning.php?";
            operation = "pla";
            var obj:Object = new Object();
            obj.ids = toSend;
            trace("ids : "+toSend);
            validateService.send(obj);
        }
        
        public function getResultTopServer(toSend:String):void{
            trace("appel de addFavoris");
            validateService.cancel();
            validateService.url = "http://valentin.lecavelier.free.fr/addFavoris.php?";
            operation = "af";
            var obj:Object = new Object();
            var tab:Array = toSend.split("|");
            obj.id = tab[0];
            obj.nom = tab[1];
            obj.ajout = tab[2];
            trace("param : "+tab);
            validateService.send(obj);
        }
        
		public function getResultSearch(str:String):void{
			validateService.cancel();
			validateService.url = URL;
			validateService.url+="GetSeries.php"
			operation = "grs";
			var req:Object= new Object();;
			req.seriesname= str;
			validateService.send(req);
		}
		
		public function getRss():void{
			validateService.cancel();
			validateService.url = URL;
			operation = "rss";
			validateService.send();
		}
		
		public function getSerieById(id:String):void{
			validateService.cancel();
			validateService.url = "http://thetvdb.com/api/"+APIKEY+"/series/"+id+"/fr.xml";
			operation = "id";
			validateService.send();
		}
		
		public function getmirror():void{
			validateService.cancel();
			validateService.url = URL;
			operation = "gm";
			validateService.url += APIKEY+"/";
			validateService.url += "mirrors.xml";
			validateService.send();
		}
		
		public function ResultHandler(event:ResultEvent):void{
			if(event.result != null){
				var data:XML = event.result as XML;
                switch(operation){
                    case "gm":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTMIRROR,data));
                        break;
                    case "grs":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTSEARCH, data));	
                        break;
                    case "gf":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTTOP, data));
                        break;
                    case "af":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTTOPSERVER, data));
                        break;
                    case "rss":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTRSS, data));
                        break;
                    case "id":
                        operation ="";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTID, data));
                        break;
                    case "pla":
                        operation = "";
                        dispatchEvent(new ServiceEvent(ServiceEvent.RESULTPLANNING, data));
                        break;
                    default:
                        //LOL
                        break;   
                }				
			}
		}
		
		public function FaultHandler(event:FaultEvent):void{
			dispatchEvent(new ErrorEvent(ErrorEvent.ERROR,event.fault.message));
		}
	}
}